﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCS.ComLib.Config;
using VietThuongLMS.Services;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Utils;
using System.Web.Security;
using VietThuongLMS.Areas.Admin.Models;

namespace VietThuongLMS.Areas.Admin.Controllers
{

    public class PhanCongController : Controller
    {
        //
        // GET: /Admin/PhanCong/
        //[Authorize(Roles = "Admin")]
        //[HttpGet, ValidateInput(false)]
        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Index(int maLopHoc)
        {
            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            PhanCongViewModel PhanCongList = PhanCongService.GetPhanCongList(9999, page, maLopHoc);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "PhanCong", "Index", "?maLopHoc="+maLopHoc+"&p=", PhanCongList.Total, page, 9999, "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            return View(PhanCongList);
        }

        //
        // GET: /Admin/PhanCong/Details/5
        //[Authorize(Roles = "Admin")]
        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Detail(int id, int maLopHoc)
        {
            LMSDataEntities context = new LMSDataEntities();
            PhanCongMoRongModel viewModel = new PhanCongMoRongModel();

            viewModel.Pc = context.PhanCongs.Join(context.LopHocs,
                pc => pc.MaLopHoc,
                lh => maLopHoc,
                (pc, lh) => pc).Where(pc => pc.ID == id).FirstOrDefault();


                //context.GiaoViens,
                //pc => pc.MaGiaoVien,
                //gv => gv.ID,
                //(pc, gv) => pc).Join(context.CaHocs,
                //pc => pc.MaCaHoc,
                //ch => ch.ID,
                //(pc, ch) => pc).Join(context.LopHocs,
                //pc => pc.MaLopHoc,
                //l => maLopHoc,
                //(pc, l) => l).Join(context.MonHocs,
                //l => l.MaMonHoc,
                //m => m.ID,
                //(l, m) => l).Join(context.LoaiLopHocs,
                //l => l.MaLoaiLopHoc,
                //llh => llh.ID,
                //(l, llh) => l).Where(l => l.ID == id).FirstOrDefault();

            ////viewModel.PhanCongViewModel = PhanCongService.GetPhanCongById(id);
            //viewModel.PhanCongViewModel = context.PhanCongs.Join(context.PhanCongs,
            //    pc => pc.MaPhanCong,
            //    l => l.ID,
            //    (pc, l) => pc).Join(context.GiaoViens,
            //    pc => pc.MaGiaoVien,
            //    gv => gv.ID,
            //    (pc, gv) => pc).Join(context.CaHocs,
            //    pc => pc.MaCaHoc,
            //    ch => ch.ID,
            //    (pc, ch) => pc).FirstOrDefault();

            return View(viewModel);
        }

        ////
        //// GET: /Admin/PhanCong/Create
        //[Authorize(Roles = "Admin")]
        //public ActionResult Create()
        //{
        //    using (LMSDataEntities context = new LMSDataEntities())
        //    {
        //        PhanCong lh = new PhanCong();
        //        List<object> values = new List<object>();
        //        values.Add(new { ID = "Thu2", Name = "Thứ 2" });
        //        values.Add(new { ID = "Thu3", Name = "Thứ 3" });
        //        values.Add(new { ID = "Thu4", Name = "Thứ 4" });
        //        values.Add(new { ID = "Thu5", Name = "Thứ 5" });
        //        values.Add(new { ID = "Thu6", Name = "Thứ 6" });
        //        values.Add(new { ID = "Thu7", Name = "Thứ 7" });
        //        values.Add(new { ID = "CN", Name = "Chủ Nhật" });

        //        IEnumerable<SelectListItem> loaiLH = new SelectList(context.LoaiPhanCongs.ToList(), "ID", "MoTa");
        //        ViewData["LoaiLH"] = loaiLH;

        //        IEnumerable<SelectListItem> maMH = new SelectList(context.MonHocs.ToList(), "ID", "TenMonHoc");
        //        ViewData["MaMH"] = maMH;

        //        IEnumerable<SelectListItem> maGV = new SelectList(context.GiaoViens.Join(context.TaiKhoans, a => a.MaTaiKhoan, t => t.ID,
        //            (a, t) => new
        //            {
        //                GiaoVienID = a.ID,
        //                GiaoVienName = t.HoTen
        //            }
        //        ).ToList(), "GiaoVienID", "GiaoVienName");

        //        ViewData["MaGV"] = maGV;

        //        ViewData["Thu"] = new SelectList(values, "ID", "Name");

        //        IEnumerable<SelectListItem> caHoc = new SelectList(context.CaHocs.ToList(), "ID", "TenCaHoc");
        //        ViewData["CaHoc"] = caHoc;

        //        return View();
        //    }
        //} 

        ////
        //// POST: /Admin/PhanCong/Create
        //[Authorize(Roles = "Admin")]
        //[HttpPost, ValidateInput(false)]
        //public ActionResult Create(PhanCongModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        try
        //        {
                    
        //            // TODO: Add insert logic here
        //            PhanCong data = new PhanCong();
        //            Converter cv = new Converter();
        //            DateTime ngayKhaiGiang = cv.StringToDate(model.NgayKhaiGiang);

        //            data.NgayKhaiGiang = ngayKhaiGiang;
        //            data.NgayKetThuc = null;
        //            //data.NgayKetThuc = model.NgayKetThuc;
        //            data.TinhTrang = Constant.SAP_MO;
        //            data.TenPhanCong = model.TenPhanCong;
        //            data.NgonNgu = model.NgonNgu;
        //            data.MaLoaiPhanCong = int.Parse(model.LoaiLH);
        //            data.MaMonHoc = int.Parse(model.MaMH);
        //            data.Deleted = Constant.GENERAL_STATUS_NORMAL;

        //            int maPhanCong = PhanCongService.InsertPhanCongReturn(data);
        //            if (maPhanCong != Constant.RETURNCODE_ERROR)
        //            {
        //                //Sau khúc này, thì nó insert vào db đúng iu cầu trong bảng Phân Công rồi
        //                //Insert vao bang phan cong 12 dong tuong ung vs 12 buoi
        //                bool r = PhanCongcService.InsertPhanCongTKB(maPhanCong, int.Parse(model.MaGV), int.Parse(model.CaHoc),
        //                                    model.Thu.ToString(), model.NgayKhaiGiang.ToString());
        //                if(r)
        //                    return RedirectToAction("Index", "PhanCong", new { area = "Admin" });
        //            }
        //            using (LMSDataEntities context = new LMSDataEntities())
        //            {
        //                List<object> values = new List<object>();
        //                values.Add(new { ID = "Thu2", Name = "Thứ 2" });
        //                values.Add(new { ID = "Thu3", Name = "Thứ 3" });
        //                values.Add(new { ID = "Thu4", Name = "Thứ 4" });
        //                values.Add(new { ID = "Thu5", Name = "Thứ 5" });
        //                values.Add(new { ID = "Thu6", Name = "Thứ 6" });
        //                values.Add(new { ID = "Thu7", Name = "Thứ 7" });
        //                values.Add(new { ID = "CN", Name = "Chủ Nhật" });

        //                IEnumerable<SelectListItem> loaiLH = new SelectList(context.LoaiPhanCongs.ToList(), "ID", "MoTa");
        //                ViewData["LoaiLH"] = loaiLH;

        //                IEnumerable<SelectListItem> maMH = new SelectList(context.MonHocs.ToList(), "ID", "TenMonHoc");
        //                ViewData["MaMH"] = maMH;

        //                IEnumerable<SelectListItem> maGV = new SelectList(context.GiaoViens.Join(context.TaiKhoans, a => a.MaTaiKhoan, t => t.ID,
        //                    (a, t) => new
        //                    {
        //                        GiaoVienID = a.ID,
        //                        GiaoVienName = t.HoTen
        //                    }
        //                ).ToList(), "GiaoVienID", "GiaoVienName");

        //                ViewData["MaGV"] = maGV;

        //                ViewData["Thu"] = new SelectList(values, "ID", "Name");

        //                IEnumerable<SelectListItem> caHoc = new SelectList(context.CaHocs.ToList(), "ID", "TenCaHoc");
        //                ViewData["CaHoc"] = caHoc;

        //                return View(model);
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            DCS.ComLib.File.Log.Write("PhanCongController::Create", ex.StackTrace, ex.Message);
        //            return View("Create", model);
        //        }
        //    }
        //    //Invalid, redisplay form with errors
        //    return View("Create", model);
        //}

        //
        // GET: /Admin/PhanCong/Edit/5
        //[Authorize(Roles = "Admin")]
        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Edit(int id, int maLopHoc)
        {
            LMSDataEntities context = new LMSDataEntities();
            PhanCong pc = new PhanCong();

            pc = context.PhanCongs.Join(context.LopHocs,
                p => p.MaLopHoc,
                lh => maLopHoc,
                (p, lh) => p).Where(p => p.ID == id).FirstOrDefault();
                //Join(context.GiaoViens,
                //p => p.MaGiaoVien,
                //gv => gv.ID,
                //(p, gv) => p).Join(context.CaHocs,
                //p => p.MaCaHoc,
                //ch => ch.ID,
                //(p, ch) => p).Where(p => p.ID == id).FirstOrDefault();

            //pc.CaHocReference.Load();
            pc.GiaoVienReference.Load();

            List<object> values = new List<object>();
            values.Add(new { ID = "Thứ 2", Name = "Thứ 2" });
            values.Add(new { ID = "Thứ 3", Name = "Thứ 3" });
            values.Add(new { ID = "Thứ 4", Name = "Thứ 4" });
            values.Add(new { ID = "Thứ 5", Name = "Thứ 5" });
            values.Add(new { ID = "Thứ 6", Name = "Thứ 6" });
            values.Add(new { ID = "Thứ 7", Name = "Thứ 7" });
            values.Add(new { ID = "Chủ Nhật", Name = "Chủ Nhật" });

                //ViewData["LoaiLH"] = new SelectList(context.LoaiPhanCongs.ToList(), "ID", "MoTa", lh.MaLoaiPhanCong);
                //ViewData["MaMH"] = new SelectList(context.MonHocs.ToList(), "ID", "TenMonHoc", lh.MaMonHoc);
                ViewData["MaGV"] = new SelectList(context.GiaoViens.Join(context.TaiKhoans, a => a.MaTaiKhoan, t => t.ID,
                    (a, t) => new
                    {
                        MaGiaoVien = a.ID,
                        GiaoVienName = t.HoTen
                    }
                ).ToList(), "MaGiaoVien", "GiaoVienName");

                ViewData["Thu"] = new SelectList(values, "ID", "Name", pc.Thu.ToString());
                //ViewData["CaHoc"] = new SelectList(context.CaHocs.ToList(), "ID", "TenCaHoc");
                //ViewData["CaHoc"] = new SelectList(context.CaHocs.Select(s => new
                //{
                //    ID = s.ID,
                //    TenCaHoc = s.ThoiGianBatDau + " - " + s.ThoiGianKetThuc,
                //}
                //).ToList(), "ID", "TenCaHoc");

            return View(pc);
        }


        // POST: /Admin/PhanCong/Edit/5
        //[Authorize(Roles = "Admin")]
        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(PhanCong pc)
        {
            try
            {
                // TODO: Add update logic here
                PhanCongService.Edit(pc);

                //return View(lh);
                return RedirectToAction("Index", "PhanCong", new { area = "Admin", @maLopHoc = pc.MaLopHoc });
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("PhanCongController::Edit", ex.StackTrace, ex.Message);
                return View(pc);
                //return RedirectToAction("View", "PhanCong", new { area = "Admin", @maLopHoc = pc.MaLopHoc });
            }
        }

        //// POST: /Admin/PhanCong/Delete/5
        //[Authorize(Roles = "Admin")]
        //[HttpGet]
        //public ActionResult Delete(int id)
        //{
        //    // TODO: Add delete logic here

        //    bool result = PhanCongService.DeletePhanCong(id);
        //    if (result)
        //    {
        //        return RedirectToAction("Index", "PhanCong", new { area = "Admin" });
        //    }
        //    else
        //    {
        //        return View();
        //    }
        //}

        //[HttpPost]
        //public bool CheckValid(string tenPhanCong)
        //{
        //    if(PhanCongService.GetPhanCong(tenPhanCong) != null)
        //        return false;
        //    return true;
        //}

        //public ActionResult Search()
        //{
        //    return View();
        //}

        //public ActionResult SearchResult(string s)
        //{
        //    LMSDataEntities context = new LMSDataEntities();
        //    var lh = context.PhanCongs.Where(a => a.TenPhanCong.Contains(s) || a.TinhTrang.Contains(s) || a.NgonNgu.Contains(s)
        //        || a.LoaiPhanCong.MoTa.Contains(s)).ToList();

        //    PhanCongViewModel lhm = new PhanCongViewModel(lh, lh.Count());
        //    return PartialView(lhm);
        //}
    }
}
